#include "sequential_stack.hpp"
#include <iostream>
#include <string.h>
using namespace std;



Sequential_stack::Sequential_stack(DataType max_size) : max_size{max_size}, sequential_stack{new DataType[max_size]{} }
{
    // this->max_size = max_size;
    this->top = -1;
    // this->sequential_stack = new DataType[max_size]{};
    cout << "Sequential_stack()" << endl;

}
Sequential_stack::Sequential_stack(const Sequential_stack &stack) : max_size{stack.max_size}, top{stack.top}, sequential_stack{new DataType[stack.max_size]{}}
{
    memcpy(this->sequential_stack, stack.sequential_stack, sizeof(DataType) * stack.max_size);
    cout << "copy constructor" << endl;
}
bool Sequential_stack::push(DataType data)
{
    if (is_full())
    {
        cout << "栈满" << endl;
        // return false;
        DataType *temp = new DataType[this->max_size * 2]{};
        this->max_size = this->max_size * 2;
        memcpy(temp, this->sequential_stack, sizeof(DataType) * this->top + 1);
        delete[] this->sequential_stack;
        this->sequential_stack = temp;// 在指向新的内存 但不能释放

    }    
    this->sequential_stack[++this->top] = data;
    return true;
}
DataType Sequential_stack::pop()
{
    if (is_empty())
    {
        cout << "栈空" << endl;
        return -1;
    }
    return this->sequential_stack[this->top--];
}
DataType Sequential_stack::get_top()
{
    if (is_empty())
    {
        cout << "栈空" << endl;
        return -1;
    }
    return this->sequential_stack[this->top];
}

Sequential_stack::~Sequential_stack()
{
    delete[] this->sequential_stack;
    this->sequential_stack = NULL;
    this->top = -1;
    this->max_size = 0;
    cout << "~Sequential_stack()" << endl;
}

bool Sequential_stack::is_empty()
{
    return this->top < 0;
}
bool Sequential_stack::is_full()
{
    return this->top >= this->max_size - 1;
}
DataType Sequential_stack::get_size()
{
    return this->top + 1;
}
DataType Sequential_stack::get_max_size()
{
    return this->max_size;
}

DataType main()
{
    Sequential_stack stack{5};
    cout << "栈空" << stack.is_empty() << endl;
    cout << "栈满" << stack.is_full() << endl;
    cout << "栈大小" << stack.get_size() << endl;
    cout << "栈容量" << stack.get_max_size() << endl;



    for (int i = 0; i < 8; i++)
    {
        stack.push(i+1);
    }
    cout << "栈空" << stack.is_empty() << endl;
    cout << "栈满" << stack.is_full() << endl;
    cout << "栈大小" << stack.get_size() << endl;
    cout << "栈容量" << stack.get_max_size() << endl;


    {
        Sequential_stack stack2{stack};
        Sequential_stack stack3 = stack;
    }
    while (!stack.is_empty())
    {
        cout << stack.pop() << endl;
    }
    cout << "栈空" << stack.is_empty() << endl;
    cout << "栈满" << stack.is_full() << endl;
    cout << "栈大小" << stack.get_size() << endl;
    cout << "栈容量" << stack.get_max_size() << endl;




}
